package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.courses.CoursePriceListBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.entity.courses.TeacherListBean;
import com.bangdream.michelia.utils.ImageLoadUtils;
import com.bangdream.michelia.utils.PublicUtil;
import com.bangdream.michelia.view.activity.currency.MainActivity;
import com.bangdream.michelia.view.activity.curriculum.CurriculumDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * @date on 2018/6/18 15:04
 * @email 909732184@qq.com
 * 课程网格列表页的适配器
 */
public class CurriculumGridItemAdapter extends RecyclerView.Adapter {
    private List<CoursesBean> data = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;
    private OnViewItemClickListener onViewItemClickListener;
    public OnViewItemClickListener getOnViewItemClickListener() {
        return onViewItemClickListener;
    }

    public void setOnViewItemClickListener(OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }

    public List<CoursesBean> getData() {
        return data;
    }

    public void setData(List<CoursesBean> data) {
        this.data = data;
    }

    public interface OnViewItemClickListener{
        void onCurriculumItemClick(int position);
    }
    public CurriculumGridItemAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.curriculum_grid_adapter_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = null;
        if(holder instanceof ViewHolder){
            //int num=(int)(Math.random()*urls.length);
            viewHolder = (ViewHolder) holder;
            viewHolder.position = position;

            CoursesBean bean = data.get(position);
            TeacherListBean teacherBean =
                    (bean.getTeacherList()==null||bean.getTeacherList().size()<1)?null:bean.getTeacherList().get(0);
            List<CoursePriceListBean> priceListBean = bean.getCoursePriceList();
            //因为价格数据是个集合 所以循环计算一下价格
            Double nmPrice = 0.0;
            if(priceListBean!=null){
                for (int i = 0;i<priceListBean.size();i++){
                    nmPrice += priceListBean.get(i).getPrice();
                }
            }
            ImageLoadUtils.loadRoundCornerImage(context,bean.getFaceUrl(),viewHolder.ivIcon);
            viewHolder.tvTeacherName.setText("讲师："+(teacherBean==null?"":teacherBean.getTeacherName()+""));
            viewHolder.tvTitle.setText(bean.getName()+"");
            viewHolder.tvPrice.setText("¥"+nmPrice);
            viewHolder.tvWatchNumber.setText(""+bean.getLearnCount());
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private ImageView ivIcon;
        private TextView tvPrice;
        private TextView tvTitle;
        private TextView tvTeacherName;
        //观看人数 学习人数
        private TextView tvWatchNumber;
        //放置图片和价格的布局控件高度动态计算得出
        private RelativeLayout relatLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            tvPrice = itemView.findViewById(R.id.tvPrice);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvTeacherName = itemView.findViewById(R.id.tvTeacherName);
            tvWatchNumber = itemView.findViewById(R.id.tvWatchNumber);
            relatLayout = itemView.findViewById(R.id.relatLayout);
            ViewGroup.LayoutParams params = relatLayout.getLayoutParams();
            //(212/350) = 0.6
            params.height = (int)((MainActivity.screenWidth/2 - PublicUtil.dip2px(context,20))*0.6);
            //布局和图片都要重新设置高度
            // 只设置图片在努比亚z17s上出现布局高度不对导致价格在图片下面
            // 只设置布局会导致图片高度不对
            relatLayout.setLayoutParams(params);
            ivIcon.setLayoutParams(params);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, CurriculumDetails.class);
                    intent.putExtra("id",data.get(position).getId());
                    intent.putExtra("title",data.get(position).getName());
                    context.startActivity(intent);
                    if(onViewItemClickListener!=null){
                        onViewItemClickListener.onCurriculumItemClick(position);
                    }
                }
            });

        }
    }
}
